enum MODEL{
    //%block="ip地址"
    0,
    //%block="子网掩码"
    1,
    //%block="网关"
    2
},
enum TYPE{
    //%block="主题"
    "topic",
    //%block="消息"
    "msg"
},
enum RTYPE{
    //%block="人脸检测"
    "FACE_RECO",
    //%block="人脸分析"
    "FACE_ATTRIBUTE",
    //%block="人体检测"
    "BODY_RECO",
    //%block="图像抄表"
    "AMMETER_READ",
    //%block="内容测评"
    "IDENTIFY_PORN",
    //%block="车牌信息"
    "NUMBER_PLATE_RECOGNITION",
    //%block="宠物种类"
    "CAT_DOG_DETECTION",
    //%block="火灾检测"
    "FIRE_DETECTION",
    //%block="安全帽检测"
    "HAT_RECO"

    

}

//% color="#1E90FF" iconWidth=50 iconHeight=40
namespace Network{
    //% block="连接wifi 名称[SSID] 密码[PASS]" blockType="command"
    //% SSID.shadow="string"  SSID.defl="Erised"
    //% PASS.shadow="string"  PASS.defl="yiruosi88"
    export function ConnectWifi(parameter: any, block: any) {
        let ssid = parameter.SSID.code; 
        let pass = parameter.PASS.code; 
        Generator.addImport("from network_esp32 import wifi");
        Generator.addInitHeader('wifiinit',`if wifi.isconnected() == False:
    for i in range(5):
        try:
            wifi.reset(is_hard=True)
            print('try AT connect wifi...')
            wifi.connect(${ssid}, ${pass})
            if wifi.isconnected():
                break
        except Exception as e:
            print(e)
                `);
    
    }
    //% block="已连接到wifi" blockType="boolean"
    
    export function isGetwifi(parameter: any, block: any) {
        Generator.addCode(`wifi.isconnected()`);
   
    }

    //% block="获取wifi的[MODEL]" blockType="reporter"
    //% MODEL.shadow="dropdown" MODEL.options="MODEL" MODEL.defl="MODEL.ip地址"  
    export function GetwifiMsg(parameter: any, block: any) {
        let model = parameter.MODEL.code;

        Generator.addCode(`wifi.ifconfig()[${model}]`);
   
    }

     //% block="---"
     export function noteSep() {

    }
    //% block="Easyiot Client [CID]iot_id[IID]iot_pwd [IPWD]" blockType="command" 
    
    //% CID.shadow="string" CID.defl="0805e3d04f3b34e8"
    //% IID.shadow="string" IID.defl="uqtPhtNGg"
    //% IPWD.shadow="string" IPWD.defl="XqtE2tNMRz"

    export function EasyIotInit(parameter: any, block: any) {
         
        let cid = parameter.CID.code;
        let iid = parameter.IID.code;
        let ipwd = parameter.IPWD.code;
        

    
        Generator.addImport("from simple import MQTTClient"); 
       // Generator.addCode(`c = miot.init_MQTT_client(${did}, ${ip}, ${pid}, ${api}, ${topic}, OneNET_recv)`);
       
        Generator.addInitHeader('EasyIotinit',`mqtt = MQTTClient(${cid}, "182.254.130.180", 1883, ${iid}, ${ipwd}, keepalive=0)`);
       
   }
   //% block="连接Easyiot  " blockType="command"    
   export function EasyiotConnect(parameter: any, block: any) {
   


    Generator.addInitHeader('connectInit',`try:
    mqtt.connect()
    print('Connected')
except:
    print('Disconnected')`);
   
}
     //% block="断开Easyiot  " blockType="command"    
   export function EasyiotDisConnect(parameter: any, block: any) {
   


    Generator.addCode(`mqtt.disconnect()`);
   
}

    //% block="向主题[TOPIC] 发送消息[MSG] " blockType="command"   
    //% TOPIC.shadow="string" TOPIC.defl="TOPIC"
    //% MSG.shadow="string" MSG.defl="HELLO"
   export function EasyiotPublish(parameter: any, block: any) {
   
    let topic = parameter.TOPIC.code;  
    let msg = parameter.MSG.code;

    Generator.addCode(`mqtt.publish1( ${topic},${msg})`);
   
}
    //% block="订阅主题[TOPIC] " blockType="command"   
    //% TOPIC.shadow="string" TOPIC.defl="TOPIC"
    
   export function EasyiotSubscribe(parameter: any, block: any) {
   
    let topic = parameter.TOPIC.code;  
    

    Generator.addInitTail("EasyIotSUB",`mqtt.subscribe(${topic})`,true);
    Generator.addInitTail('EasyIottimer',`def timer_tick(_):
        mqtt.ping()`,true);
    Generator.addInitTail('EasyIotSettimer',`tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=1, unit=Timer.UNIT_S, callback=timer_tick, arg=None, start=False, priority=1, div=0)`,true);
        
   
}
    //% block="当从主题接收数据  " blockType="hat"  
    export function EasyIotReceive(parameter: any, block: any) {
       
        Generator.addImport("from machine import Timer"); 
        Generator.addEvent("mqttcallback","mqtt_callback",`_topic,_msg`,true);
        Generator.addDeclaration("mess","global  msg,topic");
        Generator.addInit('EasySetcall',`mqtt.set_callback(mqtt_callback)`,true);
        Generator.addCode(`topic = _topic.decode('utf-8', 'ignore')
msg = _msg.decode('utf-8', 'ignore')`);
        
       

        
   }
   //% block="接收[TYPE]  " blockType="reporter"  
   //% TYPE.shadow="dropdown" TYPE.options="TYPE" RTYPE.defl="TYPE.主题" 
   export function EasyIotRecTopMsg(parameter: any, block: any) {
       let  type    = parameter.TYPE.code; 
       type = replace(type);
    
       Generator.addCode(`${type}`);
    
   

    
}
    //% block="等待Easyiot主题消息" blockType="command" 
    export function EasyIotCheck(parameter: any, block: any) { 
            
        Generator.addCode(`mqtt.wait_msg()`);

    }

     //% block="---"
     export function noteSep2() {

    }
     //% block="---"
     export function noteSep3() {

    }


    //% block="OneNet 设置设备 ID [DID]产品ID[PID]产品ApiKey [API]主题[TOPIC]" blockType="command" 
    //% DID.shadow="string" DID.defl="741475259"
    
    //% PID.shadow="string" PID.defl="444916"
    //% API.shadow="string" API.defl="bBSwXZb4jwBMBWIOIUxBB649a3A="
    //% TOPIC.shadow="string" TOPIC.defl="light"
   
    export function OneNetInit(parameter: any, block: any) {
           
         let did = parameter.DID.code;
         let pid = parameter.PID.code;
         let api = parameter.API.code;
         let topic = parameter.TOPIC.code;
         

         
         Generator.addImport("import simple,miot"); 
        // Generator.addCode(`c = miot.init_MQTT_client(${did}, , ${pid}, ${api}, ${topic}, OneNET_recv)`);
        
        Generator.addInit('Onenetinit',`c = miot.init_MQTT_client(${did}, "183.230.40.39", ${pid}, ${api}, ${topic}, OneNET_recv)`);
        
    }
    //% block="向OneNet发送数据流 名称[NAME]值 [VALUE]   " blockType="command" 
    //% NAME.shadow="string" NAME.defl="light"  
    //% VALUE.shadow="string" DAY.defl=""
    
    export function OneNetsend_data(parameter: any, block: any) {
        let name = parameter.NAME.code; 
        let value = parameter.VALUE.code;  
        

        Generator.addCode(`c.publish({${name}:${value}})`,false);
          
   }
   //% block="向OneNet发送数据流 字典[MESS]   " blockType="command" 
    //% MESS.shadow="dictionary" MESS.defl="'light':1"  
       
    export function OneNetsend_dataList(parameter: any, block: any) {
        let mess = parameter.MESS.code;      
         Generator.addCode(`c.publish(${mess})`,false);          
   }

     //% block="当从onenet接收数据  " blockType="hat"    
     export function OneNetReceive(parameter: any, block: any) {
        Generator.addDeclaration("mess","global  mess");
        Generator.addEvent("OneNetReceive","OneNET_recv",`_topic,_msg`);

        Generator.addCode(`mess=_msg.decode('utf-8', 'ignore')`);
       

        
   }
    
    //% block="获取消息" blockType="reporter"  
  
    export function OneNetSpecificReceive(parameter: any, block: any) {
       
        Generator.addCode(`mess`);
        
    }
    
    //% block="OneNet断开连接   " blockType="command" 
    export function OneNetDisconnect(parameter: any, block: any) { 
       
        Generator.addCode(`c.do_disconnect()`);
    
}
    //% block="等待Onenet主题消息" blockType="command" 
    export function OneNetCheck(parameter: any, block: any) { 
        
        Generator.addCode(`c.wait_msg()`);
    
}

function replace(str :string) {
    return str.replace("\"", "").replace("\"", "");
}
    
    //% block="---"
    export function noteSep1() {

    }
     //% block="---"
     export function noteSep4() {

    }
    //% block="获取AIOnenet鉴权码 账号[SSID] 密码[PWD] " blockType="reporter" 
    //% SSID.shadow="string" SSID.defl="user"  
    //% PWD.shadow="string" PWD.defl="pwd"  
        export function GetOneNetToken(parameter: any, block: any) { 
        let ssid = parameter.SSID.code; 
        let pwd = parameter.PWD.code; 
        Generator.addImport("import aionenet");
        Generator.addCode(`aionenet.token(${ssid},${pwd})`);
        
    }
        //% block="调用AIOnenet 图像[IMAGE] 识别[RTYPE] 授权码[TOKEN] " blockType="reporter" 
    //% IMAGE.shadow="string" IMAGE.defl="image"  
    //% RTYPE.shadow="dropdown" RTYPE.options="RTYPE" RTYPE.defl="RTYPE.人脸检测"  
    //% TOKEN.shadow="string" TOKEN.defl="token"   
    export function GetOneNetApi(parameter: any, block: any) { 
        let image = parameter.IMAGE.code; 
        let rtype = parameter.RTYPE.code; 
        let token = parameter.TOKEN.code; 
        image = replace(image);
        Generator.addCode(`aionenet.post_ai(${image},${rtype},${token})`);
        
    }



}
    